/*
 * @Author: '浪川' '1214391613@qq.com'
 * @Date: 2025-07-04 16:09:52
 * @LastEditors: '浪川' '1214391613@qq.com'
 * @LastEditTime: 2025-08-20 17:03:42
 * @FilePath: \passiflora-edulis-sims\src\boot\PuzzleApi.ts
 * @Description: 后端和拼图有关的API
 *
 * Copyright (c) 2025 by '1214391613@qq.com', All Rights Reserved.
 */

import type {
  IApiResponse,
  IWordSearchRes,
  v1ICrosswordRes,
} from 'src/interfaces/Responses/Game'
import { api } from './axios'
import type { v1IWordSearchReq } from 'src/interfaces/Requests/v1IPuzzleRes'
import type {
  AiPuzzleReq,
  IAiCrossWordRes,
  IAiDoublePuzzleRes,
} from 'src/interfaces/Responses/AIRes'
// TOOD 缺少错误捕获语句
export function v1_crossword(words: string[]) {
  return api.post<IApiResponse<v1ICrosswordRes>>(
    '/v1/crossword',
    { words: words },
    {
      headers: {
        accept: 'application/json',
        'Content-Type': 'application/json',
      },
    }
  )
}
/**
 * 生成单词搜索游戏
 * @param request 请求参数
 * @returns Promise<WordSearchData | null> 成功返回数据，失败返回 null
 */
export async function v1_wordsearch(
  request: v1IWordSearchReq
): Promise<IApiResponse<IWordSearchRes>> {
  try {
    const response = await api.post<IApiResponse<IWordSearchRes>>(
      '/v1/wordsearch',
      request,
      {
        headers: {
          accept: 'application/json',
          'Content-Type': 'application/json',
        },
      }
    )
    return response.data
  } catch (error) {
    console.error('[API Error] 生成单词搜索失败:', error)
    return {
      status_code: 404,
      message: '生成单词搜索失败',
      data: {} as IWordSearchRes,
      time: 1324,
    }
  }
}
/**
 * 生成双谜题
 * @param request 请求参数
 * @returns Promise<DoublePuzzleData | null> 成功返回数据，失败返回null
 */
export async function v1_ai_double_puzzle(
  request: AiPuzzleReq
): Promise<IApiResponse<IAiDoublePuzzleRes[]>> {
  try {
    const response = await api.post<IApiResponse<IAiDoublePuzzleRes[]>>(
      '/v1/ai/double-puzzle',
      request,
      {
        headers: {
          accept: 'application/json',
          'Content-Type': 'application/json',
        },
      }
    )
    return response.data
  } catch (error) {
    console.error('[API Error] 生成双谜题失败:', error)
    const res: IApiResponse<IAiDoublePuzzleRes[]> = {
      status_code: 404,
      message: String(error),
      data: [],
      time: 0,
    }
    return res
  }
}
export async function v1_ai_crossword(
  request: AiPuzzleReq
): Promise<IApiResponse<IAiCrossWordRes[]>> {
  try {
    const response = await api.post<IApiResponse<IAiCrossWordRes[]>>(
      '/v1/ai/crossword',
      request,
      {
        headers: {
          accept: 'application/json',
          'Content-Type': 'application/json',
        },
      }
    )
    return response.data
  } catch (error) {
    console.error('[API Error] 生成纵横拼图失败:', error)
    const res: IApiResponse<IAiCrossWordRes[]> = {
      status_code: 404,
      message: String(error),
      data: [],
      time: 0,
    }
    return res
  }
}
